package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.imall.util.JSONResult;
import com.situ.imall.util.LayUiTableDataUtil;
import com.situ.imall.util.RedisConstant;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.Product;
import com.situ.mall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName IProductServiceImpl
 * @Description TODO
 * @Author 周轩霆
 * @Date 2022/8/22 10:51
 **/
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Product selectById(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayUiTableDataUtil selectByPage(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Product> list = productMapper.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();
        return LayUiTableDataUtil.ok((int) totalCount, list);
    }

    @Override
    public LayUiTableDataUtil addProduct(Product product) {
        //保存到Redis一个Set  与上传的图片进行对比 垃圾清除
        redisTemplate.opsForSet().add(RedisConstant.UPLOAD_IMAGE_TO_DB, product.getMainImage());
        productMapper.insert(product);
        return LayUiTableDataUtil.ok("添加成功！");
    }

    @Override
    public LayUiTableDataUtil deleteProductById(Integer id) {
        productMapper.deleteByPrimaryKey(id);
        return LayUiTableDataUtil.ok("删除成功!");
    }

    @Override
    public JSONResult deleteAll(String[] idArray) {
        productMapper.deleteAll(idArray);
        return JSONResult.ok("删除成功！");
    }
}
